package com.two.shopping.service.serviceImpl;

import com.two.shopping.anno.Log;
import com.two.shopping.mapper.IShoppingCartMapper;
import com.two.shopping.mapper.IShoppingCenterMapper;
import com.two.shopping.pojo.Commodity;
import com.two.shopping.pojo.Orders;
import com.two.shopping.pojo.ShoppingCart;
import com.two.shopping.pojo.User;
import com.two.shopping.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements IShoppingCartService {
    @Autowired
    IShoppingCartMapper shoppingCartMapper;
    @Autowired
    IShoppingCenterMapper shoppingCenterMapper;

    /*这个方法是向购物车里添加商品，接收ShoppingCartController所传递过来的参数
    并调用shoppingCenterMapper(即商城模块的Dao层)中的根据Id进行查询，查询到
    用户购买的商品的名称和单价，并将以处理后封装至ShoppongCart对象中,然后传给
     shoppingCartMapper(购物车Dao)中调用里面的添加至购物车的方法

    * */
    @Transactional(rollbackFor = Exception.class)//开启事务
    @Override
    public void addCart(ShoppingCart shoppingCart) {
        //设置创建时间，即当前本机时间，并封装至shoppingCart对象中
        shoppingCart.setCreateTime(LocalDateTime.now());
        //设置修改时间，即当前本机时间，并封装至shoppingCart对象中
        shoppingCart.setUpdateTime(LocalDateTime.now());
        //调用shoppingCenterMapper中的根据Id进行查询，并保存至commodity中
        Commodity commodity = shoppingCenterMapper.idList(shoppingCart.getCommodityId());
        log.info("commodity{}", commodity);
        //获取该商品的价格并乘以单价保存至price对象中
        float price = commodity.getPrice() * shoppingCart.getQuantity();
        log.info("price{}", price);
        shoppingCart.setPrice(price);

        //获取该商品的名称并保存至price对象中
        String name = commodity.getName();
        shoppingCart.setCommodityName(name);
        //打印一下日志
        log.info("状态为{}", commodity);
        //数据库=数据库-购物车商品数量
        commodity.setQuantity(commodity.getQuantity() - shoppingCart.getQuantity());
        if (commodity.getQuantity() < 0) {
            throw new IllegalArgumentException("商品不够了");
        }
        //把数据库的商品数量改变
        shoppingCartMapper.minus(commodity);
        //添加至购物车
        shoppingCartMapper.addCart(shoppingCart);
    }

    //该方法用于查询用户购物车里的商品，参数是Id
    @Override
    public List<ShoppingCart> findAll(User user) {
        return shoppingCartMapper.findAll(user);
    }

    //删除id为#{id}的内容
    @Override
    public void deleteCartId(ShoppingCart shoppingCart) {
        //调用shoppingCenterMapper中的根据Id进行查询，并保存至commodity中
        //shoppingCart里面只有一个id属性
        ShoppingCart shoppingCart1 = shoppingCartMapper.findId(shoppingCart);
        Commodity commodity = shoppingCenterMapper.idList(shoppingCart1.getCommodityId());
        log.info("commodity{}", commodity);
        log.info("shoppingCart.getCommodityId(){}", shoppingCart1.getCommodityId());
        //数据库=数据库+购物车商品数量
        commodity.setQuantity(commodity.getQuantity() + shoppingCart1.getQuantity());
        //把数据库的商品数量改变
        shoppingCartMapper.minus(commodity);
        //移除至购物车
        shoppingCartMapper.deleteCartId(shoppingCart1);

    }

    //结算id为#{id}的内容
    @Transactional(rollbackFor = Exception.class)//开启事务
    @Override
    public void shoppingCartIdOkOrders(ShoppingCart shoppingCart1) {
        ShoppingCart shoppingCart = shoppingCartMapper.findId(shoppingCart1);
        Orders orders = new Orders();
        orders.setUserId(shoppingCart.getUserId());
        orders.setCommodityId(shoppingCart.getCommodityId());
        orders.setPrice(shoppingCart.getPrice());
        orders.setQuantity(shoppingCart.getQuantity());
        orders.setCreateTime(LocalDateTime.now());
        orders.setUpdateTime(LocalDateTime.now());
        shoppingCartMapper.shoppingCartIdOkOrders(orders);
        shoppingCartMapper.deleteCartId(shoppingCart);
    }
}
